package com.achun.ximalayamvvm

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.content.ContentValues.TAG
import android.content.Intent
import android.content.res.ColorStateList
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.animation.LinearInterpolator
import android.widget.TableLayout
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.achun.ximalayamvvm.bean.User
import com.achun.ximalayamvvm.databinding.ActivityMainBinding
import com.achun.ximalayamvvm.view.adapter.ViewPageAdapter
import com.achun.ximalayamvvm.view.fragment.HistoryFragment
import com.achun.ximalayamvvm.view.fragment.RecommendFragment
import com.achun.ximalayamvvm.view.fragment.SubscribeFragment
import com.achun.ximalayamvvm.vm.MusicIndexViewModel
import com.achun.ximalayamvvm.vm.PlayerViewModel
import com.bumptech.glide.Glide
import com.google.android.material.tabs.TabLayout
import java.text.SimpleDateFormat

class MainActivity : AppCompatActivity(){
    private lateinit var model:MusicIndexViewModel
    private lateinit var contentView:ActivityMainBinding
    private lateinit var rotateAnimation:ObjectAnimator



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        var inflate = DataBindingUtil.setContentView<MainActivityBinding>(this,R.layout.activity_main)
        contentView= DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
        initView()
        initViewModel()
    }

    private fun initViewModel() {
        var playerViewModel = PlayerViewModel.instace
        //监听当前播放歌曲
        playerViewModel.getTrack().run {
            observeForever() {
                //更新当前页面
                com.achun.ximalayamvvm.utils.LogUtil.e(this, "更新页面")
                contentView?.run {
                    playerTitle.text = it.trackTitle
                    Glide.with(contentView.root).load(it.coverUrlSmall).into(playerCover)
                }
            }
        }
        //监听进度条及播放时长

        //监听播放状态
        playerViewModel.getPlayStatus().run {
            observeForever() {
                if (it) {
                    contentView!!.playerPausePlay.setImageResource(R.drawable.selecter_pause_btn)
                    if (!rotateAnimation.isStarted)
                        rotateAnimation.start()
                    else
                        rotateAnimation.resume()

                } else {
                    contentView!!.playerPausePlay.setImageResource(R.drawable.selecter_play_btn)
                    if (rotateAnimation.isRunning){
                        rotateAnimation.pause()
                    }
                }
            }
        }
        //初始化事件
        initEvent(playerViewModel)
    }

    private fun initEvent(playerViewModel: PlayerViewModel) {
        //暂停播放事件
        contentView.playerPausePlay.setOnClickListener{
            var isPlay = playerViewModel.playorPause()
            com.achun.ximalayamvvm.utils.LogUtil.e(this, "音乐是否播放--$isPlay")
            if (isPlay)
                contentView.playerPausePlay.setImageResource(R.drawable.selecter_pause_btn)
            else
                contentView.playerPausePlay.setImageResource(R.drawable.selecter_play_btn)
        }
        contentView.root.setOnClickListener{

            startActivity(Intent(this,PlayerActivity::class.java))
        }
    }

    private fun initView() {
        //跑马灯需要点击
        contentView.playerTitle.isSelected=true
        //初始化属性动画
        rotateAnimation =
            ObjectAnimator.ofFloat(contentView.playerCover,"rotation",0.0f,360.0f)
        rotateAnimation.run {
            duration = 10*1000
            interpolator= LinearInterpolator()
            repeatCount= ObjectAnimator.INFINITE
            repeatMode= ValueAnimator.RESTART
        }
        //绑定viewpage
       contentView.tabSelectPage.setupWithViewPager(contentView.tabPage)
        var listOf = listOf<Fragment>(RecommendFragment(), SubscribeFragment(), HistoryFragment())
        contentView.tabPage.run {
            adapter=ViewPageAdapter(supportFragmentManager,listOf)
            offscreenPageLimit=2
        }

    }



    override fun onDestroy() {
        super.onDestroy()

    }

}






